/**
 * Copyright (c) qc&Bull Co.,LTD.
 * All right reserved.
 */

package cn.zxd.lis.schema;

import cn.zxd.lis.db.LogDomainToItemDB;
import cn.zxd.util.*;


import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * <p>ClassName: LogDomainToItemSchema </p>
 * <p>Description: DB层 Schema 类文件 </p>
 * <p>Company: qc&Bull Co.,LTD </p>
 * @Database: IIEP_Data_Model
 * @author：anonymous
 * @CreateDate：2015-11-22
 */
public class LogDomainToItemSchema implements Schema, Cloneable
{
    // @Field
    /** 日志主题ID */
    private String SubjectID;
    /** 日志监控点ID */
    private String ItemID;
    /** 控制点关键号码类型 */
    private String KeyType;
    /** 日志控制开关 */
    private String Switch;

    /** 数据库表的字段个数 */
    public static final int FIELDNUM = 4;

    /** 主键 */
    private static String[] PK;

    /** 错误信息 */
    public CErrors mErrors;

    // @Constructor
    public LogDomainToItemSchema()
    {
        mErrors = new CErrors();
        String[] pk = new String[2];
        pk[0] = "SubjectID";
        pk[1] = "ItemID";
        PK = pk;
    }

    /**
     * Schema克隆
     * @return Object
     * @throws CloneNotSupportedException
     */
    public Object clone() throws CloneNotSupportedException
    {
        LogDomainToItemSchema cloned = (LogDomainToItemSchema)super.clone();
        cloned.mErrors = (CErrors) mErrors.clone();
        return cloned;
    }

    // @Method
    public String[] getPK()
    {
        return PK;
    }

    public String getSubjectID()
    {
        return SubjectID;
    }

    public void setSubjectID(String aSubjectID)
    {
        SubjectID = StrTool.cTrim(aSubjectID);
    }

    public String getItemID()
    {
        return ItemID;
    }

    public void setItemID(String aItemID)
    {
        ItemID = StrTool.cTrim(aItemID);
    }

    public String getKeyType()
    {
        return KeyType;
    }

    public void setKeyType(String aKeyType)
    {
        KeyType = StrTool.cTrim(aKeyType);
    }

    public String getSwitch()
    {
        return Switch;
    }

    public void setSwitch(String aSwitch)
    {
        Switch = StrTool.cTrim(aSwitch);
    }


    /**
     * 使用另外一个 LogDomainToItemSchema 对象给 Schema 赋值
     * @param: cLogDomainToItemSchema LogDomainToItemSchema
     */
    public void setSchema(LogDomainToItemSchema cLogDomainToItemSchema)
    {
        this.SubjectID = cLogDomainToItemSchema.getSubjectID();
        this.ItemID = cLogDomainToItemSchema.getItemID();
        this.KeyType = cLogDomainToItemSchema.getKeyType();
        this.Switch = cLogDomainToItemSchema.getSwitch();
    }

    /**
     * 使用 ResultSet 中的第 i 行给 Schema 赋值
     * @param: rs ResultSet
     * @param: i int
     * @return: boolean
     */
    public boolean setSchema(ResultSet rs, int i)
    {
        try
        {
            if (rs.getString(1) == null)
                this.SubjectID = null;
            else
                this.SubjectID = rs.getString(1);
            if (rs.getString(2) == null)
                this.ItemID = null;
            else
                this.ItemID = rs.getString(2);
            if (rs.getString(3) == null)
                this.KeyType = null;
            else
                this.KeyType = rs.getString(3);
            if (rs.getString(4) == null)
                this.Switch = null;
            else
                this.Switch = rs.getString(4);
        }
        catch (SQLException sqle)
        {
            sqle.printStackTrace();
            // @@错误处理
            CError tError = new CError();
            tError.moduleName = "LogDomainToItemSchema";
            tError.functionName = "setSchema";
            tError.errorMessage = sqle.toString();
            this.mErrors.addOneError(tError);
            return false;
        }
        return true;
    }

    public LogDomainToItemSchema getSchema()
    {
        LogDomainToItemSchema aLogDomainToItemSchema = new LogDomainToItemSchema();
        aLogDomainToItemSchema.setSchema(this);
        return aLogDomainToItemSchema;
    }

    public LogDomainToItemDB getDB()
    {
        LogDomainToItemDB aDBOper = new LogDomainToItemDB();
        aDBOper.setSchema(this);
        return aDBOper;
    }

    /**
     * 数据打包，按 XML 格式打包
     * @return: String 返回打包后字符串
     */
    public String encode()
    {
        StringBuffer strReturn = new StringBuffer(256);
        strReturn.append(StrTool.cTrim(SubjectID));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(ItemID));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(KeyType));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(Switch));
        return strReturn.toString();
    }

    /**
     * 数据解包
     * @param: strMessage String 包含一条纪录数据的字符串
     * @return: boolean
     */
    public boolean decode(String strMessage)
    {
        try
        {
            SubjectID = StrTool.getStr(strMessage, 1, SysConst.PACKAGESPILTER);
            ItemID = StrTool.getStr(strMessage, 2, SysConst.PACKAGESPILTER);
            KeyType = StrTool.getStr(strMessage, 3, SysConst.PACKAGESPILTER);
            Switch = StrTool.getStr(strMessage, 4, SysConst.PACKAGESPILTER);
        }
        catch (NumberFormatException ex)
        {
            // @@错误处理
            CError tError = new CError();
            tError.moduleName = "LogDomainToItemSchema";
            tError.functionName = "decode";
            tError.errorMessage = ex.toString();
            this.mErrors.addOneError(tError);
            return false;
        }
        return true;
    }

    /**
     * 取得对应传入参数的String形式的字段值
     * @param: FCode String 希望取得的字段名
     * @return: String
     * 如果没有对应的字段，返回""
     * 如果字段值为空，返回"null"
     */
    public String getV(String FCode)
    {
        String strReturn = "";
        if (FCode.equalsIgnoreCase("SubjectID"))
        {
            strReturn = StrTool.cTrim(SubjectID);
        }
        if (FCode.equalsIgnoreCase("ItemID"))
        {
            strReturn = StrTool.cTrim(ItemID);
        }
        if (FCode.equalsIgnoreCase("KeyType"))
        {
            strReturn = StrTool.cTrim(KeyType);
        }
        if (FCode.equalsIgnoreCase("Switch"))
        {
            strReturn = StrTool.cTrim(Switch);
        }
        if (strReturn.equals(""))
        {
            strReturn = "null";
        }
        return strReturn;
    }

    /**
     * 取得Schema中指定索引值所对应的字段值
     * @param: nFieldIndex int 指定的字段索引值
     * @return: String
     * 如果没有对应的字段，返回""
     * 如果字段值为空，返回"null"
     */
    public String getV(int nFieldIndex)
    {
        String strFieldValue = "";
        switch (nFieldIndex)
        {
            case 0:
                strFieldValue = StrTool.cTrim(SubjectID);
                break;
            case 1:
                strFieldValue = StrTool.cTrim(ItemID);
                break;
            case 2:
                strFieldValue = StrTool.cTrim(KeyType);
                break;
            case 3:
                strFieldValue = StrTool.cTrim(Switch);
                break;
            default:
                strFieldValue = "";
        }
        if (strFieldValue.equals(""))
        {
            strFieldValue = "null";
        }
        return strFieldValue;
    }

    /**
     * 设置对应传入参数的String形式的字段值
     * @param: FCode String 需要赋值的对象
     * @param: FValue String 要赋的值
     * @return: boolean
     */
    public boolean setV(String FCode, String FValue)
    {
        if (StrTool.cTrim(FCode).equals(""))
            return false;

        if (FCode.equalsIgnoreCase("SubjectID"))
        {
            if (FValue != null)
            {
                SubjectID = FValue.trim();
            }
            else
                SubjectID = null;
        }
        if (FCode.equalsIgnoreCase("ItemID"))
        {
            if (FValue != null)
            {
                ItemID = FValue.trim();
            }
            else
                ItemID = null;
        }
        if (FCode.equalsIgnoreCase("KeyType"))
        {
            if (FValue != null)
            {
                KeyType = FValue.trim();
            }
            else
                KeyType = null;
        }
        if (FCode.equalsIgnoreCase("Switch"))
        {
            if (FValue != null)
            {
                Switch = FValue.trim();
            }
            else
                Switch = null;
        }
        return true;
    }

    public boolean equals(Object otherObject)
    {
        if (this == otherObject)
            return true;
        if (otherObject == null)
            return false;
        if (getClass() != otherObject.getClass())
            return false;
        LogDomainToItemSchema other = (LogDomainToItemSchema) otherObject;
        return
                (SubjectID == null ? other.getSubjectID() == null : SubjectID.equals(other.getSubjectID()))
                        && (ItemID == null ? other.getItemID() == null : ItemID.equals(other.getItemID()))
                        && (KeyType == null ? other.getKeyType() == null : KeyType.equals(other.getKeyType()))
                        && (Switch == null ? other.getSwitch() == null : Switch.equals(other.getSwitch()));
    }

    /**
     * 取得Schema拥有字段的数量
     * @return: int
     */
    public int getFieldCount()
    {
        return FIELDNUM;
    }

    /**
     * 取得Schema中指定字段名所对应的索引值
     * 如果没有对应的字段，返回-1
     * @param: strFieldName String
     * @return: int
     */
    public int getFieldIndex(String strFieldName)
    {
        if (strFieldName.equals("SubjectID"))
        {
            return 0;
        }
        if (strFieldName.equals("ItemID"))
        {
            return 1;
        }
        if (strFieldName.equals("KeyType"))
        {
            return 2;
        }
        if (strFieldName.equals("Switch"))
        {
            return 3;
        }
        return -1;
    }

    /**
     * 取得Schema中指定索引值所对应的字段名
     * 如果没有对应的字段，返回""
     * @param: nFieldIndex int
     * @return: String
     */
    public String getFieldName(int nFieldIndex)
    {
        String strFieldName = "";
        switch (nFieldIndex)
        {
            case 0:
                strFieldName = "SubjectID";
                break;
            case 1:
                strFieldName = "ItemID";
                break;
            case 2:
                strFieldName = "KeyType";
                break;
            case 3:
                strFieldName = "Switch";
                break;
            default:
                strFieldName = "";
        }
        return strFieldName;
    }

    /**
     * 取得Schema中指定字段名所对应的字段类型
     * 如果没有对应的字段，返回Schema.TYPE_NOFOUND
     * @param: strFieldName String
     * @return: int
     */
    public int getFieldType(String strFieldName)
    {
        if (strFieldName.equals("SubjectID"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("ItemID"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("KeyType"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("Switch"))
        {
            return Schema.TYPE_STRING;
        }
        return Schema.TYPE_NOFOUND;
    }

    /**
     * 取得Schema中指定索引值所对应的字段类型
     * 如果没有对应的字段，返回Schema.TYPE_NOFOUND
     * @param: nFieldIndex int
     * @return: int
     */
    public int getFieldType(int nFieldIndex)
    {
        int nFieldType = Schema.TYPE_NOFOUND;
        switch (nFieldIndex)
        {
            case 0:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 1:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 2:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 3:
                nFieldType = Schema.TYPE_STRING;
                break;
            default:
                nFieldType = Schema.TYPE_NOFOUND;
        }
        return nFieldType;
    }
}
